//提示信息对象
let msg;
//列对象
let cols;
layui.config({
    base: '../lay/dist/'
});
layui.use(['form', 'table', 'laydate', 'notice'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        laydate = layui.laydate,
        notice = layui.notice;
    msg = layui.notice;
    /**
     * 表格主题渲染
     */
    table.render({
        elem: '#checkHealth',
        url: '/feedback/all',
        method: "post",
        parseData: function (res) {
            return {
                "code": res.success == true ? 0 : 1,
                "count": res.total,
                "data": res.data

            }
        },
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [
            [
                {
                    field: 'xid', width: 80, title: '序号', sort: true, templet: function (d) {
                        return d.LAY_INDEX;
                    }
                },
                {field: 'id', width: 100, title: 'ID', sort: true, hide: true},

                {
                  field: "userId",align: "center",title: "用户Id",templet(d){
                      return  '<a class="showTips" href="403/images/qyn.jpg" style="    display: block;\n' +
                          '    text-decoration: revert;color: #00a2d4">'+d.userid+'</a>'
                    }
                },

                {
                    field: 'createtime', width: 180, sort:true,align: 'center', title: '反馈时间', templet: function (d) {
                        return layui.laytpl.toDateString(d.date);
                    }
                },

                {
                  field: 'content',align: 'center',title: '反馈内容'
                },
                {
                    field: 'image',width: 200,title: '图片',align: 'center',templet:function (result) {
                        let image = result.image.toString();
                        if (image.length <= 0) {
                            return "<span>" + '∑(っ°Д°;)っ卧槽，不见了' + "</span>";
                        }
                        let htmlScriptElement="";
                        let array = new Array();
                        array = image.split(",");

                        for (let i = 0; i < array.length; i++) {
                            htmlScriptElement += '<span><img src="' + array[i] + '" alt="" style="height: auto;width: auto; max-width: 100%; max-height: 100%;padding-left: 5px" onclick="showBigImage(this)""></span>';
                        }
                        return htmlScriptElement;
                    }
                },
                {
                    field: 'state',title: '反馈状态',align: 'center',templet(d){
                        switch (d.state) {
                            case -1:
                                return '未处理'
                            case 0:
                                return  '处理中'
                            default:
                                return '处理完成'
                        }
                    }
                },
                {
                     align: 'center',title: '反馈类型', templet(d) {
                        return d.feedType.name
                    }
                },
                {
                    field: 'feedType', align: 'center',title: '反馈目标',templet(d){
                        switch (d.feedType.isSystem) {
                            case -1:
                                return '前台'
                            case 0:
                                return  '后台'
                            default:
                                return '学校'
                        }
                    }
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}

            ]
        ],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true,
        skin: 'line',
        done:function () {
            showTips(); //显示提示文字
            $(".search_c").click(function () {

            })
        }
    });
    /**
     * 监听搜索操作
     */
    form.on('submit(data-search-btn)', function (data) {
        let result = data.field;
        console.log("处理前",result)
        if (result.name.length==0) {
            // delete result.name;
        }

        console.log(result.name);
// return false
        //执行搜索重载
        table.reload('checkHealth', {
            url: '/feedback/search'
            , where: {
                name:result.name,
                state:result.state,
                isSystem:result.isSystem
            }
        }, 'data');
        notice.success("搜索执行成功！");

        return false;
    });
    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'selectAll') { //
            table.reload('checkHealth', {
                url: '/feedback/all'
            })
            notice.success("查询成功！");
        }
    });
    /**
     * 工具行监听(删除/编辑)
     * */
    table.on('tool(currentTableFilter)', function (obj) {

        let data = obj.data;
        cols = obj;
        if (obj.event === 'edit') {
            console.log(data)
            console.log(data.id);
            $.post("/feedback/dispose",{id:data.id},function (res) {
                if (res.success) {
                    obj.update({
                        "state":data.state+1
                    }) //修改当前行数据
                    notice.success("处理成功")
                } else {
                    notice.error("处理失败"+res.data)
                }
            })
            // let index = layer.open({
            //     title: '编辑用户',
            //     type: 2,
            //     shade: 0.2,
            //     maxmin: true,
            //     shadeClose: true,
            //     area: ['100%', '100%'],
            //     content: '/CheckHealthEdit?id=' + data.id,
            // });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            console.log("删除")
            layer.confirm('是否要删除此条记录？', function (index) {
                $.post("/feedback/del", {id: obj.data.id}, function (result) {
                    // console.log(result)

                    if (result.success!=true) {
                        notice.warning("删除失败！"+result.data);
                    } else {
                        notice.success("删除成功！");
                        obj.del()
                    }
                },'json')
                layer.close(index);
            }, function (index) {
                layer.close(index);
            });

        }

    });
    /**
     * 时间戳的处理
     * @param d
     * @param format
     * @returns {string}
     */
    layui.laytpl.toDateString = function (d, format) {
        let date = new Date(d || new Date())
            , ymd = [
            this.digit(date.getFullYear(), 4)
            , this.digit(date.getMonth() + 1)
            , this.digit(date.getDate())
        ]
            , hms = [
            this.digit(date.getHours())
            , this.digit(date.getMinutes())
            , this.digit(date.getSeconds())
        ];

        format = format || 'yyyy-MM-dd hh:mm:ss ';

        return format.replace(/yyyy/g, ymd[0])
            .replace(/MM/g, ymd[1])
            .replace(/dd/g, ymd[2])
            .replace(/hh/g, hms[0])
            .replace(/mm/g, hms[1])
            .replace(/ss/g, hms[2]);
    };
    /**
     * 数字前置补零
     * @param num
     * @param length
     * @param end
     * @returns {*}
     */
    layui.laytpl.digit = function (num, length, end) {
        let str = '';
        num = String(num);
        length = length || 2;
        for (let i = num.length; i < length; i++) {
            str += '0';
        }
        return num < Math.pow(10, length) ? str + (num | 0) : num;
    };
    /**
     * 时间选择器自定义颜色
     */
    laydate.render({
        elem: '#beginTime'
        , theme: '#393D49'
    });


    /**
     * 设置提示信息属性
     * @type {{hideEasing: string, positionClass: string, hideDuration: string, debug: boolean, closeButton: boolean, extendedTimeOut: string, showEasing: string, onclick: null, showDuration: string, iconClass: string, timeOut: string}}
     */
    notice.options = {
        closeButton: true,//显示关闭按钮
        debug: false,//启用debug
        positionClass: "toast-bottom-right",//弹出的位置,
        showDuration: "300",//显示的时间
        hideDuration: "1000",//消失的时间
        timeOut: "2000",//停留的时间
        extendedTimeOut: "1000",//控制时间
        showEasing: "swing",//显示时的动画缓冲方式
        hideEasing: "linear",//消失时的动画缓冲方式
        iconClass: 'toast-info', // 自定义图标，有内置，如不需要则传空 支持layui内置图标/自定义iconfont类名
        onclick: null, // 点击关闭回调
    };

    // notice.warning("");信息状态
    // notice.info("");
    // notice.error("");
    // notice.success("");

});

/**
 * 图片缩放
 * @param e
 */
function showBigImage(e) {
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        shadeClose: true, //点击阴影关闭
        maxWidth:500,
        maxHeight: 500,
        content: "<img style='height: auto;width: auto; max-width: 100%; max-height: 100%;  ' src=" + $(e).attr('src') + " />"
    });
}

function showTips() {
$('.showTips').mouseenter(
function(){
    let that = this;
    layer.tips('点击查看用户详情信息.', that,{
        time:1000
    }); //在元素的事件回调体中，follow直接赋予this即可
})
}